
#include <stdio.h>
#include "debug.h"
#include "ZlibInterface.h"

ZlibInterface *ZlibInterface::zlib_iface_singleton = NULL;

/** Get singleton instance (prevents double initialisation) */
ZlibInterface &ZlibInterface::getInstance()
{
   if (zlib_iface_singleton == NULL)
      zlib_iface_singleton = new ZlibInterface();
   return *zlib_iface_singleton;
}

/** Initialise the zlib library. */
ZlibInterface::ZlibInterface()
{
   // doesn't need global initialisation!
}

/** Destructor for the class, also clears, destroys and frees the
 * library structures. */
ZlibInterface::~ZlibInterface()
{
   // shouldn't need global destruction
}

/** Wrapper function for generating textual errors from Zlib library
 * error codes.
 * 
 * @param status Zlib library error code.
 * 
 * @param fpretty Pretty printed function name where the error
 * originated from.
 * 
 * @param line Line number where the error originated from. */
void ZlibInterface::func_CheckZlibError(int status, const char *fpretty, int line)
{
   // end of stream is not really an error message...
   if ((status == Z_OK) || (status == Z_STREAM_END)) return;
   const char *terror = "";
   switch (status)
   {
      case Z_OK: terror = "ok"; break;
      case Z_STREAM_END: terror = "end of stream"; break;
      case Z_NEED_DICT: terror = "the operation required a dictionary"; break;
      case Z_ERRNO: terror = "errno needs to be queried"; break;
      case Z_STREAM_ERROR: terror = "stream error"; break;
      case Z_DATA_ERROR: terror = "data error"; break;
      case Z_MEM_ERROR: terror = "memory error"; break;
      case Z_BUF_ERROR: terror = "buffer error"; break;
      case Z_VERSION_ERROR: terror = "version mismatch"; break;
   }
   errprintpd("zlib - %s", fpretty, line, terror);
}

/** Destroy the singleton. */
void ZlibInterface::destroyInstance()
{
   delete zlib_iface_singleton;
   zlib_iface_singleton = NULL;
}
